﻿using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class DashboardPresenter : BasePresenter
    {
        private readonly IDashboardView _view;
        private readonly Administrator _administrator;

        public DashboardPresenter(IDashboardView view)
            : this(view, new RepositoryFactory())
        {
        }
        
        public DashboardPresenter(IDashboardView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
        }

        public void Initialize()
        {
            // Use the absence of master templates as an indicator of entering a new installation
            if (_administrator.CurrentSite.MasterTemplates.Count == 0)
            {
                DashboardGadgetHandler.SyncGadgets(SystemRepository);
            }
            _view.SetGadgets(SystemRepository.GetDashboardGadgets());
        }

        public Administrator Administrator
        {
            get { return _administrator; }
        }
    }
}